package com.example.caoh.common.util;



import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

/**
 * @author wuxi
 */
public class ElsBool {


	public List<JSONObject> must;
	public List<JSONObject> must_not;
	public List<JSONObject> should;

	public void setMust(List<JSONObject> must) {
		this.must = must;
	}

	public void setMust_not(List<JSONObject> must_not) {
		this.must_not = must_not;
	}

	public void setShould(List<JSONObject> should) {
		this.should = should;
	}

	public List<JSONObject> getMust() {
		return must;
	}

	public List<JSONObject> getMust_not() {
		return must_not;
	}

	public List<JSONObject> getShould() {
		return should;
	}

	public void add(ElsMethod method, ElsOp op, JSONObject q) {
		JSONObject obj = new JSONObject();
		obj.put(op.toString(), q);
		switch (method) {
		case Must:
			if (must == null)
				must = new ArrayList<JSONObject>();
			must.add(obj);
			break;
		case MustNot:
			if (must_not == null)
				must_not = new ArrayList<JSONObject>();
			must_not.add(obj);
			break;
		case Should:
			if (should == null)
				should = new ArrayList<JSONObject>();
			should.add(obj);
			break;
		default:
			break;
		}
	}

	public void add(ElsMethod method, ElsOp op, String key, String value) {
		JSONObject q = new JSONObject();
		q.put(key, value);
		add(method, op, q);
	}
	
	public void add(ElsMethod method, ElsOp op, String key, String[] value) {
		JSONObject q = new JSONObject();
		q.put(key, value);
		add(method, op, q);
	}

	public void add(ElsBool bool, ElsMethod method) {
		JSONObject obj = new JSONObject();
		obj.put("bool", bool);
		switch (method) {
		case Must:
			if (must == null)
				must = new ArrayList<JSONObject>();
			must.add(obj);
			break;
		case MustNot:
			if (must_not == null)
				must_not = new ArrayList<JSONObject>();
			must_not.add(obj);
			break;
		case Should:
			if (should == null)
				should = new ArrayList<JSONObject>();
			should.add(obj);
			break;
		default:
			break;
		}
	}

	public JSONObject findRangeObj(String key, ElsMethod method) {
		switch (method) {
		case Must:
			return findRange(must, key);
		case MustNot:
			return findRange(must_not, key);
		case Should:
			return findRange(should, key);
		default:
			return null;

		}
	}

	public void addRange(String key, Object value, ElsRange range, ElsMethod method) {
		JSONObject obj = this.findRangeObj(key, ElsMethod.Must);
		if (obj != null) {
			obj.put(range.toString(), value);
		} else {
			obj = new JSONObject();
			JSONObject ran = new JSONObject();
			ran.put(range.toString(), value);
			obj.put(key, ran);
			this.add(method, ElsOp.Range, obj);
		}
	}

	private JSONObject findRange(List<JSONObject> list, String key) {
		if (null == list)
			return null;
		for (JSONObject obj : list) {
			if (obj.get("range") != null) {
				if (obj.getJSONObject("range").get(key) != null) {
					return obj.getJSONObject("range").getJSONObject(key);
				}
			}
		}
		return null;
	}

}